var app = getApp();
Page({
    data:{
        siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
        curPage : '/zofui_sales/sort/sort',
        page : {
            isend : false,
            doing : false,
            doo : 'sort_v1',
            pdata : {
                op : 'list',
                page : 1,
                sortid : 0, // 分类id
                sorttwo : 0,
                otype : 0,
                gtype : 0,
            },
            waitf : 0,
            nodataf : 0
        },
        datalist : [],
        sort : [],
        leftact : 0,
        color : '#ffffff',
        activeIndex:0,
        shopid : 0,
        searchs : '',
        cart : 0,
        issearch : false,
        scrolltop : 0,
        isShowCart: false
    },
    onLoad:function(options){
        var self = this;

        var zfid = options.zfid ? options.zfid : '';
        var shopid = options.shopid ? options.shopid : '';
        self.setData({
            options : options,
        });
        app.com.auth(function(user){
            app.com.http('sort_v1','POST',{op:'sortlist',shopid:shopid},0,false,'','',function(res){
                if (!res.data.errno && res.data) {
                    var color = app.com.mainColor(res.data.data.params.style);
                    self.setData({
                        color : color,
                        sort : res.data.data.sort,
                        shopid : shopid,
                        inited : true,
                        params : res.data.data.params,
                        activeIndex : res.data.data.actindex,
                        cart : res.data.data.cart ? res.data.data.cart : {cartnum:0},
                    },function(){
                        var title = res.data.data.params.title ? res.data.data.params.title : '分类';
                        title = shopid > 0 && self.data.cart ? self.data.cart.shopname : title;
                        wx.setNavigationBarTitle({
                            title:  title,
                        });
                        // 初始化页面获取购物车信息
                        self.getCartShop()
                    });

                }else{
                    app.com.alert(res.data.message,function(){
                        app.com.backPage(0);
                    });
                }
            });
			app.com.setBar(app,self,shopid);
        })
    },
    onShareAppMessage : function(){
        var self = this;
        return {
            title: self.data.sharedata.title,
            path: self.data.sharedata.path,
            imageUrl : self.data.sharedata.img,
        }
    },
    onPullDownRefresh: function () {
        if( this.data.isdown ) return false;
        this.data.isdown = true;
        this.onLoad( this.data.options );
        wx.stopPullDownRefresh();
        this.data.isdown = false;
    },
    actfn(e){
        var self = this;
        self.setData({
            activeIndex : e.currentTarget.dataset.index
        })
    },
    actfnat(e){
        this.setData({
            activeIndex : e.currentTarget.dataset.index,
        })
        if(this.data.issearch){
            this.searchfn('show');
        }
        // wx.pageScrollTo({
        //     scrollTop: 0
        // })
    },
    falsesearch(e){
        this.searchfn('show');
    },
    searchfn(e){
        for (var i = 0; i < this.data.sort.length; i++) {
            for (var j = 0; j < this.data.sort[i].down.length; j++) {
                var item = this.data.sort[i].down[j];
                var ishideitem = 1;
                if( item.title.indexOf(e.detail) >= 0 || e == 'show' || !e.detail ){
                    ishideitem = 0;
                }
                this.setData({
                    ['sort['+i+'].down['+j+'].ishideitem'] : ishideitem
                })
            }
        }
        this.setData({
            searchs : e.detail ? e.detail : '',
            issearch : e.detail ? true : false,
        })
    },
    showselect(){
        this.setData({
            showselect : !this.data.showselect
        })
    },
    // 加入购物车
    setCartnum(e){
        var now = e.detail.num*1;
        var added = now - this.data.cart.allnum*1;
        this.setData({
            'cart.cartnum' : added > 0 ? this.data.cart.cartnum*1 + added : this.data.cart.cartnum,
            'cart.allnum' : now,
        })
        // 获取购物车商品
        this.getCartShop()
    },
    location : function( e ){
        app.com.location( e );
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    navigateto : function( e ){
        app.com.navigateto( e );
    },
    redirectto : function( e ){
        app.com.redirectto( e );
    },    
    callphone : function(e){
        app.com.callphone( e );
    },
    toweburl : function(e){
        app.com.toweburl( e );
    },
    showimages : function( e ){
        app.com.showimages( e );
    },
    footNavigate(e){
        var url = e.currentTarget.dataset.url;
        wx.switchTab({
          url: url
        })
    },
    // 显示购物车列表
    showCart() {
        this.setData({isShowCart: true});
        wx.stopPullDownRefresh()
    },
    // 隐藏购物车列表
    hideCart() {
        this.setData({isShowCart: false});
        wx.startPullDownRefresh()
    },
    // 获取购物车信息
    getCartShop(){
        var self = this
        var options = this.options
		var shopid = options.shopid ? options.shopid : '';
        var zfid = '';
        app.com.auth(function(user){
            app.com.getLocation(self,function(loc){
                var postdata = {
                    op:'info',
                    lat : loc.latitude,
                    lng : loc.longitude,
                    zfid:zfid,
                    shopid:shopid,
                }
                app.com.http('cart','POST',postdata,0,false,'','',function(res){
                    if (!res.data.errno && res.data) {
                        self.setData({
                            'cart.goodlist' : res.data.data.goodlist,
                        });
                    }
                });
            })
        });
    },
    // 结算
    countit(){
        var self = this;
        var arr = [];
        var k=0,l=0;
        for (var i = 0; i < self.data.cart.goodlist.length; i++) {
            var item = self.data.cart.goodlist[i].list;
            for (var j = 0; j < item.length; j++) {
                var ina = {cid:item[j].cid,num:item[j].num*1};
                arr.push( ina );
                k=i;
                l=j;
            }
        }

        if( arr.length < 1 ){
            app.com.toast('还没选择');return false;
        }else if(arr.length == 1){
            var item = self.data.cart.goodlist[k].list[l];
            wx.setStorageSync('zfssbuy',{'gid':item.id,type:0,mapid:item.ruleid ? item.ruleid : '',mapname:item.rulename ? item.rulename : '',num:item.num});

        }else{
            wx.setStorageSync('zfssbuy',{arr:arr,type:'cart'});
        }
        var tourl = '/zofui_sales/order/confirm';
        wx.navigateTo({
          url: tourl
        });
    }, 
})